/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;



/**
 *
 * @author apple
 */
public class TraderImpl extends UnicastRemoteObject implements Trader{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	//private Account account;
	private List<Item> goods = new ArrayList<Item>();
	private List<Item> wish = new ArrayList<Item>();	

	protected TraderImpl(String name) throws RemoteException {		
		// TODO Auto-generated constructor stub		
		super();
		this.name = name;
		//this.account = account;
	}

	@Override
	public synchronized void addItem(String name, float price, int num, String trader) throws RemoteException {
		// TODO Auto-generated method stub
		if(!goods.containsKey(name)){
			Item new_goods = new Item(name, price, num, this.name);
			goods.put(name, new_goods);
		}

	}

	@Override
	public synchronized boolean deleteItem(String name) throws RemoteException {
		// TODO Auto-generated method stub
		Iterator<Entry<String, Item>> iterator = goods.entrySet().iterator();
		boolean hasItem = false;
		
		while(iterator.hasNext()){
			String _name = iterator.next().getKey();
			if(_name.equals(name)){
				goods.remove(_name);
				hasItem = true;
			}
		}
		
		if(!hasItem){
			throw new UnsupportedOperationException("Item does not exist.");
		}

		return hasItem;
	}

	@Override
	public synchronized List<Item> searchItem(String name) throws RemoteException {
		// TODO Auto-generated method stub
		List<Item> result  = new ArrayList<Item>();
		Iterator<Entry<String, Item>> iterator = goods.entrySet().iterator();
		
		while(iterator.hasNext()){
			Entry<String, Item> pair = iterator.next();
			String _name = pair.getKey();
			if(_name.equals(name)){
				result.add(pair.getValue());
			}
		}		
		
		return result;
	}

	@Override
	public synchronized float buyItem(Item item) throws RemoteException {
		// TODO Auto-generated method stub
		float paid = 0;
		float price = item.getPrice();
		/*float balance = account.getBalance();
		
		if( balance >= price){
			account.withdraw(price);
		}
		
		if(paid == 0){
			throw new RejectedException("Your balance is not enough.");
		}*/
		
		return paid;
	}

	@Override
	public synchronized void add2wishList(String name, float price) throws RemoteException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean sellItem(Item item) throws RemoteException {
		// TODO Auto-generated method stub
		boolean result = false;
		Iterator<Entry<String, Item>> iterator = goods.entrySet().iterator();
		
		while(iterator.hasNext()){
			Entry<String, Item> pair = iterator.next();
			String _name = pair.getKey();
			if(_name.equals(item)){
				Item match = pair.getValue();
				int num = match.getAmount();
				if(num > 0){
					match.setAmount(num - 1);
					result = true;
				}
			}
		}	
		
		if(!result){
			throw new UnsupportedOperationException("The item is out of stock.");
		}
		
		return result;
	}

	@Override
	public boolean editItem(String name, String new_name, float new_price) throws RemoteException {
		// TODO Auto-generated method stub
		Iterator<Entry<String, Item>> iterator = goods.entrySet().iterator();
		boolean hasItem = false;
		
		while(iterator.hasNext()){
			String _name = iterator.next().getKey();
			if(_name.equals(name)){
				goods.remove(_name);
				hasItem = true;
			}
		}
		
		if(!hasItem){
			throw new UnsupportedOperationException("Item does not exist.");
		}

		return hasItem;
	}

}
